package com.beauty.dressing.module.market.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.beauty.dressing.R;
import com.beauty.dressing.common.base.BaseActivity;
import com.beauty.dressing.common.bean.BaseBean;
import com.beauty.dressing.common.bean.LeaseItemDetail;
import com.beauty.dressing.common.bean.LeaseOrderResp;
import com.beauty.dressing.common.bean.StringBase;
import com.beauty.dressing.common.utils.Constants;
import com.beauty.dressing.common.view.LoadingNodataLayout;
import com.beauty.dressing.module.market.adapter.ShopDetailAdapter;
import com.beauty.dressing.module.market.holder.ShopHeaderHolder;
import com.beauty.dressing.module.market.presenter.LeaseDetailPresenter;
import com.beauty.dressing.module.market.presenter.contract.LeaseDetailContract;

import java.util.ArrayList;
import java.util.List;

/**
 * 类描述
 *
 * @author xutingz
 * @email：914603097@qq.com
 */

public class LeaseDetailActivity extends BaseActivity implements LeaseDetailContract.ViewControl {

    private ViewGroup mLeaseLayout;
    private ShopDetailAdapter mAdapter;
    private RecyclerView mRecyclerView;
    private LeaseDetailPresenter mPresenter = new LeaseDetailPresenter(this);
    private long sales_id;
    private boolean isLease = true;
    private ShopHeaderHolder mHeaderHolder;
    private LoadingNodataLayout mNodtaLayout;

    public static void startActivity(Context context, long sales_id) {
        Intent intent = new Intent(context, LeaseDetailActivity.class);
        intent.putExtra(Constants.SALES_ID, sales_id);
        context.startActivity(intent);
    }

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_lease_detail);
        Intent intent = getIntent();
        sales_id = intent.getLongExtra(Constants.SALES_ID, 0);
    }

    @Override
    public void findView() {
        TextView mTitle = (TextView) findViewById(R.id.title_txt);
        mTitle.setText("商品详情");
        findViewById(R.id.left_img_btn).setOnClickListener(this);
        findViewById(R.id.buy_lease).setOnClickListener(this);
        mLeaseLayout = (ViewGroup) findViewById(R.id.lease_layout);
        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        mNodtaLayout = (LoadingNodataLayout) findViewById(R.id.nodata_layout);
        mNodtaLayout.setCenterGravity();
        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        mRecyclerView.setLayoutManager(layoutManager);
        mAdapter = new ShopDetailAdapter(context);
        mRecyclerView.setAdapter(mAdapter);

    }

    @Override
    public void initData() {
        mHeaderHolder = new ShopHeaderHolder(context, LayoutInflater.from(context), mRecyclerView);
        mAdapter.addHeaderView(mHeaderHolder.itemView);
        mNodtaLayout.setVisibility(View.VISIBLE);
        mNodtaLayout.setIsLoadingUi(true);
        mLeaseLayout.setVisibility(View.VISIBLE);

        mPresenter.getLeaseDetail(sales_id);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.left_img_btn:
                finish();
                break;
            case R.id.buy_lease:
                showProgressDialog();
                mPresenter.createLeaseOrder(controller.getCacheManager().getLoginUser().member_id, sales_id);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPresenter.clear();
    }

    @Override
    public void onLeaseDetail(boolean isSuccess, int code, String message, LeaseItemDetail data) {
        if (isSuccess) {
            if (data != null) {
                mHeaderHolder.bindData(data);
                if (data.img != null && !data.img.isEmpty()) {
                    List<BaseBean> beanList = new ArrayList<>();
                    for (String str : data.img) {
                        beanList.add(new StringBase(str));
                    }
                    mAdapter.setChild(beanList);
                }
                mNodtaLayout.setVisibility(View.GONE);
            } else {
                mNodtaLayout.setIsLoadingUi(false);
            }
        } else {
            mNodtaLayout.setIsLoadingUi(false);
        }
    }

    @Override
    public void onLeaseOrder(boolean isSuccess, int code, String message, LeaseOrderResp data) {
        dismissProgressDialog();
        if (isSuccess && data != null) {
//            WriteOrderActivity.startActivity(context,data.goods,data.defaAddress != null && !data.defaAddress.isEmpty()?data.defaAddress.get(0):null);
            WriteOrderActivity.startActivity(context, data.goods, data.defaAddress);
        } else {
            showToast("下单失败");
        }

    }
}
